/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.UserAddressPo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  UserAddressPoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class UserAddressSqlProvider {
	public static final String TABLE_NAME = "user_address";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param userAddress
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserAddresss(final UserAddressPo userAddress) {
		return new CustomerSQL() {{
			SELECT("address,city_id,city_name,contactor,create_time,creator_id,description,id,phone,province_id,province_name,update_time,updater_id,user_id,user_name,default_addr,country_name,country_id ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userAddress.getCityId())) {
				WHERE("city_id=#{cityId}");
			}
			if (StringUtils.isNotBlank(userAddress.getContactor())) {
				WHERE("contactor=#{contactor}");
			}
			if (null != userAddress.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userAddress.getPhone())) {
				WHERE("phone=#{phone}");
			}
			if (StringUtils.isNotBlank(userAddress.getProvinceId())) {
				WHERE("province_id=#{provinceId}");
			}
			if (StringUtils.isNotBlank(userAddress.getCountryId())) {
				WHERE("country_id=#{countryId}");
			}
			if (null != userAddress.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != userAddress.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userAddress.getUserName())) {
				WHERE("user_name=#{userName}");
			}
			if (null != userAddress.getDefaultAddr()) {
				WHERE("default_addr=#{defaultAddr}");
			}
			if(StringUtils.isNotBlank(userAddress.getSortField())) {
				ORDER_BY(userAddress.getSortField() + (StringUtils.isNotBlank(userAddress.getSortMethod()) ? "desc" : userAddress.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != userAddress.getStart() && null != userAddress.getSize()) {
				LIMIT(userAddress.getStart(), userAddress.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param userAddress
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserAddressCount(final UserAddressPo userAddress) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(userAddress.getAddress())) {
				WHERE("address=#{address}");
			}
			if (StringUtils.isNotBlank(userAddress.getCityId())) {
				WHERE("city_id=#{cityId}");
			}
			if (StringUtils.isNotBlank(userAddress.getCityName())) {
				WHERE("city_name=#{cityName}");
			}
			if (StringUtils.isNotBlank(userAddress.getContactor())) {
				WHERE("contactor=#{contactor}");
			}
			if (null != userAddress.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userAddress.getDescription())) {
				WHERE("description=#{description}");
			}
			if (StringUtils.isNotBlank(userAddress.getPhone())) {
				WHERE("phone=#{phone}");
			}
			if (StringUtils.isNotBlank(userAddress.getProvinceId())) {
				WHERE("province_id=#{provinceId}");
			}
			if (StringUtils.isNotBlank(userAddress.getProvinceName())) {
				WHERE("province_name=#{provinceName}");
			}
			if (null != userAddress.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if (null != userAddress.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userAddress.getCountryId())) {
				WHERE("country_id=#{countryId}");
			}
			if (StringUtils.isNotBlank(userAddress.getUserName())) {
				WHERE("user_name=#{userName}");
			}
			if (null != userAddress.getDefaultAddr()) {
				WHERE("default_addr=#{defaultAddr}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserAddressById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param userAddress
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addUserAddress(final UserAddressPo userAddress) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(userAddress.getAddress())) {
				VALUES("address","#{address}");
			}
			if (StringUtils.isNotBlank(userAddress.getCityId())) {
				VALUES("city_id","#{cityId}");
			}
			if (StringUtils.isNotBlank(userAddress.getCityName())) {
				VALUES("city_name","#{cityName}");
			}
			if (StringUtils.isNotBlank(userAddress.getContactor())) {
				VALUES("contactor","#{contactor}");
			}
			if (null != userAddress.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(userAddress.getDescription())) {
				VALUES("description","#{description}");
			}
			if (StringUtils.isNotBlank(userAddress.getPhone())) {
				VALUES("phone","#{phone}");
			}
			if (StringUtils.isNotBlank(userAddress.getProvinceId())) {
				VALUES("province_id","#{provinceId}");
			}
			if (StringUtils.isNotBlank(userAddress.getProvinceName())) {
				VALUES("province_name","#{provinceName}");
			}
			if (null != userAddress.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
			if (null != userAddress.getUserId()) {
				VALUES("user_id","#{userId}");
			}
			if (StringUtils.isNotBlank(userAddress.getUserName())) {
				VALUES("user_name","#{userName}");
			}
			if (null != userAddress.getDefaultAddr()) {
				VALUES("default_addr","#{defaultAddr}");
			}
			if (StringUtils.isNotBlank(userAddress.getCountryId())) {
				VALUES("country_id","#{countryId}");
			}
			if (StringUtils.isNotBlank(userAddress.getCountryName())) {
				VALUES("country_name", "#{countryName}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param userAddress
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateUserAddress(final UserAddressPo userAddress) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userAddress.getAddress())) {
				SET("address=#{address}");
			}
			if (StringUtils.isNotBlank(userAddress.getCityId())) {
				SET("city_id=#{cityId}");
			}
			if (StringUtils.isNotBlank(userAddress.getCityName())) {
				SET("city_name=#{cityName}");
			}
			if (StringUtils.isNotBlank(userAddress.getContactor())) {
				SET("contactor=#{contactor}");
			}
			if (null != userAddress.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(userAddress.getDescription())) {
				SET("description=#{description}");
			}
			if (StringUtils.isNotBlank(userAddress.getPhone())) {
				SET("phone=#{phone}");
			}
			if (StringUtils.isNotBlank(userAddress.getProvinceId())) {
				SET("province_id=#{provinceId}");
			}
			if (StringUtils.isNotBlank(userAddress.getProvinceName())) {
				SET("province_name=#{provinceName}");
			}
			if (null != userAddress.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			if (null != userAddress.getUserId()) {
				SET("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userAddress.getUserName())) {
				SET("user_name=#{userName}");
			}
			if (null != userAddress.getDefaultAddr()) {
				SET("default_addr=#{defaultAddr}");
			}
			if (StringUtils.isNotBlank(userAddress.getCountryId())) {
				SET("country_id=#{countryId}");
			}
			if (StringUtils.isNotBlank(userAddress.getCountryName())) {
				SET("country_name = #{countryName}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delUserAddress(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
